package com.voute.View.Adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.voute.Model.Bean.SimpleNotificationEntity;
import com.voute.R;
import com.voute.Utils.ViewHolderUtils;
import com.voute.View.Activity.FeedDetailInfoActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xianke on 22/04/2017.
 */

public class PushMessageAdapter extends BaseAdapter {
    private List<SimpleNotificationEntity> datas = new ArrayList<>();
    private Context context;

    public PushMessageAdapter(Context c, List<SimpleNotificationEntity> sources) {
        if (sources != null) {
            datas.clear();
            datas.addAll(sources);
            context = c;
        }
    }

    public void setDatas(List<SimpleNotificationEntity> sources) {
        if (sources != null) {
            datas.clear();
            datas.addAll(sources);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.adapter_push_message_list, null);
        }
        initView(convertView, position);
        return convertView;
    }

    private void initView(View v, int position) {
        final SimpleNotificationEntity entity = datas.get(position);
        TextView tv_push_msg = ViewHolderUtils.get(v, R.id.tv_push_msg);
        String sentence1 = "";
        if ("up".equals(entity.getPtype())) {
            sentence1 = "在话题";
        } else {
            sentence1 = "话题";
        }

        String sentence2 = "";
        sentence2 = entity.getTitle();
        String sentence3 = "";
        if ("comment".equals(entity.getPtype())) {
            sentence3 = "有新评论";
        } else if ("vote".equals(entity.getPtype())) {
            sentence3 = "有新投票";
        } else if ("up".equals(entity.getPtype())) {
            sentence3 = "下的评论有新的赞同";
        }
        String html_string = "<html><span><font color=\"#999999\">"+sentence1+"&nbsp;</font><font color=\"#33333\">"+sentence2+"&nbsp;"+"</font><font color=\"#999999\">"+sentence3+"</font></span></html>";
        Html.fromHtml(html_string);
        tv_push_msg.setText(Html.fromHtml(html_string));
        if(entity.isUnread()){
            Drawable nav_up=context.getResources().getDrawable(R.mipmap.dot_red);
            nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
            tv_push_msg.setCompoundDrawables(nav_up,null, null, null);
        }
        else{
            Drawable nav_up=context.getResources().getDrawable(R.mipmap.dot_red);
            nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
            tv_push_msg.setCompoundDrawables(null,null, null, null);
        }
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, FeedDetailInfoActivity.class);
                intent.putExtra("KEY_FEED_HASH",entity.getHash());
                context.startActivity(intent);
            }
        });
    }
}
